<?php
/**
 * 注：此模板为自动生成模板，需要根据实际情况调整修改
 * 此模板对 无主键/单一主键 表的支持较好，联合主键表在下一版本开发
 */
namespace Data;

use Utils\MyException;

class Tree_verify_code_DAO {

						public static $TABLENAME = 'Tree_verify_code';

							public static $_db;

	public static function query($str)
	{
		if(self::$_db === null) {
			self::$_db = \GatewayWorker\Lib\Db::instance('main');
			return self::$_db->query($str);
		} else {
			return self::$_db->query($str);
		}
	}

								


				public static function getRecord($CODE_ID){
						 $where = "`CODE_ID` = '$CODE_ID'";
						 		 $record = self::query('select * from '.static::$TABLENAME .' where '.$where);
								 if($record != null){
								 return new Tree_verify_code($record[0]);
				      		}
				      		 return null;
				      		 }
		


				public static function getList() {
						 $where = "";
        				 $list = self::query('select * from '.static::$TABLENAME);
						 $results = array();

						foreach($list as $record){
						  $results[] = new Tree_verify_code($record);
								}
								 return $results;
	               }



			   public static function insert($record){

						try {

								$CODE_ID = $record->getCODE_ID();
								$oldrecord = self::getRecord($CODE_ID);
								if($oldrecord != null) {
								   throw new MyException ("重复插入");
								}
								$sql = 'insert into '.static::$TABLENAME .' values ('.$record->toInsertCSV().')' ;
            					self::query($sql);
            					return self::$_db->lastInsertId();
							} catch(MyException $e){
								throw $e;
								}
						}



			   public static function update($record){
						try{
								$CODE_ID = $record->getCODE_ID();
								$where = "`CODE_ID` = '$CODE_ID'";
								$oldrecord = self::getRecord($CODE_ID);
								if($oldrecord == null){
										throw new MyException("不存在该记录");
								}
            					$sql = 'update '.static::$TABLENAME .' set '.$record->toUpdateSet().' where '.$where ;
            					self::query($sql);
						} catch(MyException $e){
										throw $e;
									}
					 }



			   public static function delete ($record){
						try{
							$CODE_ID = $record->getCODE_ID();
							$where = "`CODE_ID` = '$CODE_ID'";
							$oldrecord = self::getRecord($CODE_ID);
							if($oldrecord == null){
									throw new MyException("不存在该记录");
							}
            				$sql = 'delete from '.static::$TABLENAME . ' where '.$where ;
            				self::query($sql);
            				} catch(MyException $e){
									throw $e;
				        	}
					}
}

?>